import { message } from 'antd';
import { useEffect, useState } from 'react';
/**
 * 
 * @param cb 获取数据的函数
 * @param key 需要依赖变更的数据
 * @returns 
 */
export function useApi(cb: Function, key: any) {
    const [data, setData] = useState([] as any);
    
	useEffect(() => {
			(async () => {
				const res = await cb();
				if (res.code === 0) return setData(res.data);
				message.error(res.msg);
			})();
		},[ key ]);
	return [ data, setData ];
}


export function useApiParams(cb: Function, key: any, ...rest: any) {
    const [data, setData] = useState([] as any);
	useEffect(() => {
			(async () => {
				const res = await cb(...rest);
				if (res.code === 0) return setData(res.data);
				message.error(res.msg);
			})();
		},[ key ]);
	return [ data, setData ];
}
